using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;

using SQLString.Classes;
using SQLString.AppResource;
using SQLString.Properties;


namespace SQLString.Dialogs
{
	public partial class fSQLString : Form
	{

		DTE2 ApplicationObject;
		AddIn ApplicationINSTANCE;
		Settings mUser = new Settings();

		SQLResource SQLRes = new SQLResource();
		CommonFuntions SProfile = new CommonFuntions();
		SQLObjectManager SQLManager = null;
		StringManager SQLStrMan = null;
		TextDocument anchorPoint = null;
		//common connection for all
		string szConnection = string.Empty;
		public string Connection
		{
			get
			{
				return szConnection.Trim();
			}
			set
			{
				szConnection = value.Trim();
			}
		}
		Boolean bConstructorDone = false;

		public fSQLString(DTE2 _appOObj, AddIn _appInstc)
		{
			//		Passing the application object from connection
			if (_appOObj != null && _appInstc!=null)
			{
				ApplicationObject = _appOObj;
				ApplicationINSTANCE = _appInstc;
			}
			InitializeComponent();
			bConstructorDone = true;
		}

		private void cmdClose_Click( object sender, EventArgs e )
		{
			if (object.Equals(cmdClose.Text,"Close"))
			{
				this.Close();
			}
			else if (object.Equals(cmdClose.Text,"Cancel"))
			{
				this.cmdClose.Text = "Close";
				this.cmdClose.BackColor = SystemColors.Control;
				cmdGen.Enabled = false;
				cmdSave.Enabled = false;
				cmdReset.Enabled = false;
				cmdInsert.Enabled = false;
				rtfSQLSource.Text = string.Empty;
				rtfSQLTarget.Text = string.Empty;
				lstLang.SelectedIndex = -1;
				lstData.SelectedIndex = -1;
				txtVersion.Text = string.Empty;
				txtWrap.Text="80";
				txtSQLVar.Text = "sSQL";
				chkCase.CheckState = CheckState.Unchecked;
				chkTemplate.CheckState = CheckState.Unchecked;
			}
		}

		private void fSQLString_Load( object sender, EventArgs e )
		{
			if (SQLRes.LoadApplicationValueResources())
			{

				if (bConstructorDone)
				{
					fSQLStringSettings fProps = new fSQLStringSettings(SQLRes);
					//	Checking the propertie
					if (string.IsNullOrEmpty(mUser.DatabaseCatalog)==true)
					{
						Monitor.sMsg("SQLString Settings not established.  Please enter the required information.", MessageBoxIcon.Warning);

						DialogResult dlgShow = fProps.ShowDialog();
					}
					else if (string.IsNullOrEmpty(mUser.DatabaseCatalog)!=true)	//	The properties are populated with data
					{
						//	on load, instantiate the fSQLStringSettings UI, execute the GetSQLConnectionString() to get ODBC connect
						Connection = fProps.GetSQLConnectString();
						SQLManager = new SQLObjectManager(SQLRes, Connection);	//	Instantiating the Main SQL Interaction class with the connection and resource objects.
						if ((string.IsNullOrEmpty(Connection) != true) && (Connection!="-1") && (Connection!="-2"))
						{
							loadInitialData();  
							//	for insertion of generated code
							anchorPoint= this.ApplicationObject.ActiveDocument.Object("TextDocument") as TextDocument;	
						}
						else if (Connection == "-1")//	Connection failure issues
						{
							Monitor.sMsg("SQLString Settings for the ODBC Connection failed.  Please check Database Properties and ODBC Settings.", MessageBoxIcon.Warning);
						}
						else if (Connection == "-2")//	Database object issues
						{
							Monitor.sMsg("SQLString Settings not established.  The database has missing table objects.", MessageBoxIcon.Warning);
						}
					}
					fProps.Dispose();
				}
			}
			else
			{
				Monitor.sMsg("Problem retrieving application resources.  This is not fatal, but it is recommended that re-installation be performed.  Please reinstall.", MessageBoxIcon.Warning);
			}
		}

		private void exitToolStripMenuItem_Click( object sender, EventArgs e )
		{

			this.Close();
		}

		private void propertiesToolStripMenuItem_Click( object sender, EventArgs e )
		{
			fSQLStringSettings fProps = new fSQLStringSettings(SQLRes);
			DialogResult dlgShow = fProps.ShowDialog(this);
		}

		private void applicationSettingsToolStripMenuItem_Click( object sender, EventArgs e )
		{
			fUserAppSettings fUsrProps = new fUserAppSettings();
			DialogResult dlgShow = fUsrProps.ShowDialog(this);
			if (dlgShow == DialogResult.OK)
			{

			}
		}

		private void rtfSQLSource_TextChanged( object sender, EventArgs e )
		{
			if (( string.IsNullOrEmpty(rtfSQLSource.Text) != true ) && ( string.IsNullOrEmpty(txtSQLVar.Text) != true ) 
				&& ( lstLang.SelectedIndex >= 0 ) && ( lstData.SelectedIndex >= 0 ) && (string.IsNullOrEmpty(txtWrap.Text)!=true))
			{
				cmdClose.Text = "Cancel";
				cmdClose.BackColor = Color.Wheat;
				cmdGen.Enabled = true;
				cmdReset.Enabled = true;
				cmdSave.Enabled = true;
			}
			else if (( string.IsNullOrEmpty(rtfSQLSource.Text)) || ( string.IsNullOrEmpty(txtSQLVar.Text) )
				|| ( lstLang.SelectedIndex < 0 ) || ( lstData.SelectedIndex < 0 ) || ( string.IsNullOrEmpty(txtWrap.Text)))
			{
				cmdClose.Text = "Close";
				cmdClose.BackColor = SystemColors.Control;
				cmdGen.Enabled = false;
				cmdReset.Enabled = false;
				cmdSave.Enabled = false;
			}
			lblSQLCnt.Text = rtfSQLSource.TextLength.ToString();
		}

		private void chkCase_CheckStateChanged( object sender, EventArgs e )
		{

		}

		private void Common_EventHandler( object sender, EventArgs e )
		{
			if (( string.IsNullOrEmpty(rtfSQLSource.Text) != true ) && ( string.IsNullOrEmpty(txtSQLVar.Text) != true )
				&& ( lstLang.SelectedIndex >= 0 ) && ( lstData.SelectedIndex >= 0 ) && ( string.IsNullOrEmpty(txtWrap.Text) != true ))
			{
				cmdClose.Text = "Cancel";
				cmdClose.BackColor = Color.Wheat;
				cmdGen.Enabled = true;
				cmdReset.Enabled = true;
				cmdSave.Enabled = true;
			}
			else if (( string.IsNullOrEmpty(rtfSQLSource.Text) ) || ( string.IsNullOrEmpty(txtSQLVar.Text) )
				|| ( lstLang.SelectedIndex < 0 ) || ( lstData.SelectedIndex < 0 ) || ( string.IsNullOrEmpty(txtWrap.Text) ))
			{
				cmdClose.Text = "Close";
				cmdClose.BackColor = SystemColors.Control;
				cmdGen.Enabled = false;
				cmdReset.Enabled = false;
				cmdSave.Enabled = false;
			}
			lblSQLCnt.Text = rtfSQLSource.TextLength.ToString();

		}

		private void loadInitialData()
		{
			try
			{
				System.Collections.SortedList slLang = new System.Collections.SortedList();
				System.Collections.SortedList slDataType = new System.Collections.SortedList();
				System.Collections.SortedList slFramework = new System.Collections.SortedList();
				slLang = SQLManager.LoadCombos(lstLang, "SELECT LANG_NM, LANG_ID FROM T_LANG WHERE R_STATUS_CD='A' ORDER BY 1 ASC");
				slDataType = SQLManager.LoadCombos(lstData, "SELECT DATA_TYPE_NM, DATA_TYPE_ID FROM T_DATA_TYPES WHERE R_STATUS_CD='A' ORDER BY 1 ASC");
				//slFramework = SQLManager.LoadCombos(lstframe, "SELECT FRAMEWORK_NM, FRAMEWORK_ID FROM T_FRAMEWORK WHERE R_STATUS_CD='A' ORDER BY 1 ASC");

				lstframe.SelectedIndex = 0;
				lstData.SelectedIndex = 0;
				lstLang.SelectedIndex = 0;
			}
			catch (Exception ex)
			{

			}
		}

		private void cmdGen_Click( object sender, EventArgs e )
		{
			SQLStrMan = new StringManager();
			SQLStrMan.StringEvauation = rtfSQLSource.Text.Trim();
			SQLStrMan.UpperCase = chkCase.Checked;
			SQLStrMan.UserDataType = lstData.SelectedItem.ToString();
			SQLStrMan.UserLanguage = lstLang.SelectedItem.ToString();
			SQLStrMan.UserVariable = txtSQLVar.Text.Trim();
			SQLStrMan.Wrap = int.Parse(txtWrap.Text);
			if (SQLStrMan.ProcessSQL())
			{
				this.rtfSQLTarget.Text = SQLStrMan.StringEvauation;
			}
		}

		private void rtfSQLTarget_TextChanged( object sender, EventArgs e )
		{
			if (this.rtfSQLTarget.Text.Length > 0)
			{
				cmdInsert.Enabled = true;
			}
			else if (this.rtfSQLTarget.Text.Length == 0)
			{
				cmdInsert.Enabled = false;
			}
			this.lblGENCnt.Text = this.rtfSQLTarget.TextLength.ToString();
		}

		private void cmdInsert_Click( object sender, EventArgs e )
		{
			this.anchorPoint.Selection.Text = this.rtfSQLTarget.Text.Trim();
		}
	}
}